package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Hotel;
import com.ruoyi.system.domain.HotelBook;
import com.ruoyi.system.domain.OrderHistory;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.HotelBookMapper;
import com.ruoyi.system.mapper.HotelMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.IHotelBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 酒店预订Service业务层处理
 * 
 * @author xdh
 * @date 2024-06-25
 */
@Service
public class HotelBookServiceImpl implements IHotelBookService 
{
    @Autowired
    private HotelBookMapper hotelBookMapper;
    @Autowired
    private OrderHistoryServiceImpl orderHistoryServiceImpl;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private HotelMapper hotelMapper;

    /**
     * 查询酒店预订
     * 
     * @param bookingId 酒店预订主键
     * @return 酒店预订
     */
    @Override
    public HotelBook selectHotelBookByBookingId(Integer bookingId)
    {
        return hotelBookMapper.selectHotelBookByBookingId(bookingId);
    }

    /**
     * 查询酒店预订列表
     * 
     * @param hotelBook 酒店预订
     * @return 酒店预订
     */
    @Override
    public List<HotelBook> selectHotelBookList(HotelBook hotelBook)
    {
        //获取当前登录用户的id
        Long userId= SecurityUtils.getUserId();
        SysUserRole sysUserRole= sysUserRoleMapper.getUserRole(userId);
        if(sysUserRole.getRoleId()==101){   //101表示为游客的角色，只能看到游客自己的订单
            hotelBook.setUserId(Integer.valueOf(userId.toString()));
        }
        return hotelBookMapper.selectHotelBookList(hotelBook);
    }

    /**
     * 新增酒店预订
     * 
     * @param hotelBook 酒店预订
     * @return 结果
     */
    @Override
    public int insertHotelBook(HotelBook hotelBook)
    {
        return hotelBookMapper.insertHotelBook(hotelBook);
    }

    /**
     * 修改酒店预订
     * 
     * @param hotelBook 酒店预订
     * @return 结果
     */
    @Override
    public int updateHotelBook(HotelBook hotelBook)
    {
        if(hotelBook.getPaymentStatus().equals("1")) {
            OrderHistory orderHistory = new OrderHistory();
            orderHistory.setUserId(Long.valueOf(hotelBook.getUserId()));
            orderHistory.setBookingId(Long.valueOf(hotelBook.getBookingId()));
            orderHistory.setOrderDate(new Date());

            Hotel hotel = hotelMapper.selectHotelByHotelId(hotelBook.getHotelId());
            String detail = "这是酒店预订的订单，入住在" + hotel.getHotelName() + ",人数为：" + hotelBook.getGuestCount() + "。";
            orderHistory.setOrderDetail(detail);
            orderHistory.setOrderPrice(Long.valueOf(hotelBook.getTotalPrice()));
            orderHistoryServiceImpl.insertOrderHistory(orderHistory);
        }
        return hotelBookMapper.updateHotelBook(hotelBook);
    }

    /**
     * 批量删除酒店预订
     * 
     * @param bookingIds 需要删除的酒店预订主键
     * @return 结果
     */
    @Override
    public int deleteHotelBookByBookingIds(Integer[] bookingIds)
    {
        return hotelBookMapper.deleteHotelBookByBookingIds(bookingIds);
    }

    /**
     * 删除酒店预订信息
     * 
     * @param bookingId 酒店预订主键
     * @return 结果
     */
    @Override
    public int deleteHotelBookByBookingId(Integer bookingId)
    {
        return hotelBookMapper.deleteHotelBookByBookingId(bookingId);
    }
}
